package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Order;
import cn.wolfcode.crm.service.IOrderService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

// 销售产品统计
@Controller
@RequestMapping("saleProduct")
public class SaleProductController {

    @Autowired
    private IOrderService orderService;

    @RequestMapping("")
    public String index(Model model) {

        // 销售总金额
        BigDecimal totalMoney = new BigDecimal("0");
        // 销售总数量
        BigDecimal totalCount = new BigDecimal("0");
        // 销售产品名
        List<String> productNames = new ArrayList<>();
        // 销售数量
        List<BigDecimal> productCount = new ArrayList<>();
        // 销售排行TOP3
        List<String> sortNames = new ArrayList<>();

        List<Order> orders = orderService.selectAll();
        for (int i = 0; i < orders.size(); i++) {

            totalMoney = totalMoney.add(orders.get(i).getOutcomeBill().getTotalAmount());
            totalCount = totalCount.add(orders.get(i).getOutcomeBill().getTotalNumber());
            productNames.add(orders.get(i).getProduct().getProductName());
            productCount.add(orders.get(i).getOutcomeBill().getTotalNumber());

            if (i < 3) {
                sortNames.add(orders.get(i).getProduct().getProductName());
            }
        }

        model.addAttribute("totalmoney", totalMoney);
        model.addAttribute("totalcount", totalCount);
        model.addAttribute("productnames", JSON.toJSONString(productNames));
        model.addAttribute("names", sortNames);
        model.addAttribute("productcount", productCount);

        return "saleProduct";
    }
}
